package edu.gatech.asthma.ui_support;

import java.util.ArrayList;
import java.util.List;

import edu.gatech.asthma.R;
import edu.gatech.asthma.database.DBAdapter;
import edu.gatech.asthma.database.ProfileVO;
import edu.gatech.asthma.ui_support.login.ProfilePicPageFragment;
import edu.gatech.asthma.ui_support.login.ViewPagerAdapter;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.PorterDuff;
import android.graphics.drawable.GradientDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * Provides methods for profile customization based on user preferences
 */
public class ProfileCustomizer {
	int bgColorChosen=8;		// number representing user color choice. default is 8
	int tintColorChosen=0;		// number representing user color choice. default is 0
	int accentColorChosen=0;	// number representing user color choice. default is 0
	Activity context;			//pointer to corresponding activity context
	View layout;				//pointer to layout for altering appearance
	String packageName;			//name of package for context of Activity
	DBAdapter database;			//database adapter passed in
	int identifier;				//identifier for profile in database
	
	public ProfileCustomizer(Activity c, View l, DBAdapter d, int i) {
		context = c;
		layout = l;
		database=d;
		identifier = i;
		packageName = context.getPackageName();
	}
	
	public void setCustomizations(){
		ProfileVO profile = database.getProfile(identifier);
		setBackgroundColor(profile.getBgColor());
		
		//Special customizations for home page
		if (context.getClass().getSimpleName().equals("HomeScreenActivity")) {
			setCharacter(profile.getPic(), profile.getTintColor(), profile.getAccentColor());
			//if female set profile icon to be a girl
			if (profile.getGender() == 1) {
				LinearLayout profileIcon = (LinearLayout) layout.findViewById(R.id.home_button_profile);
				((ImageView) profileIcon.getChildAt(0)).setImageResource(R.drawable.icon_profile_girl);
			}
		}
	}
	
	
	/**
	 * Changes the character displayed in the layout of current Activity
	 * Character image must be present in an imageView called "character_img" in the layout
	 * @param color Integer representing chosen character (corresponds to colors defined in resources)
	 */
	private void setCharacter(int charNum, int tintColor, int accentColor) {
		//resource identifiers
		int rIdCharTint=ResourceHelper.getImage("character"+charNum+"_tint");
		int rIdCharAccent=ResourceHelper.getImage("character"+charNum+"_accent");	
		int rIdCharTop=ResourceHelper.getImage("character"+charNum+"_top");	
		int rIdTintColor=ResourceHelper.getColor("character_color"+tintColor);
		int rIdAccentColor=ResourceHelper.getColor("accent_color"+accentColor);
		
		ImageView imgTint = (ImageView) layout.findViewById(R.id.profile_img_tint);
		ImageView imgAccent = (ImageView) layout.findViewById(R.id.profile_img_accent);
		ImageView imgTop = (ImageView) layout.findViewById(R.id.profile_img_top);

		//set character images and tint appropriately
		imgTint.setImageResource(rIdCharTint);
		imgTint.setColorFilter(context.getResources().getColor(rIdTintColor));
		imgAccent.setImageResource(rIdCharAccent);
		imgAccent.setColorFilter(context.getResources().getColor(rIdAccentColor));
		imgTop.setImageResource(rIdCharTop);
		
	}
	
	/**
	 * Sets the currently selected color options. Used on the profile edit screen
	 * to set previously selected colors as defaults
	 * @param bg background color identifier
	 * @param tint character color identifier
	 * @param accent character accent color identifier
	 */
	public void setColors(int bg, int tint, int accent){
		bgColorChosen = bg;
		tintColorChosen = tint;
		accentColorChosen = accent;
	}
	
	/**
	 * A helper method that accesses gets all character drawables and
	 * populates Fragments (layouts) that will be displayed in the ViewPager on the customize screen
	 * @return profiles A list of Fragments with all profile information filled in
	 */
	public List<Fragment> getCharacterImgs() {
		List<Fragment> img = new ArrayList<Fragment>();
		int rIdTint=0,rIdAccent=0,rIdTop=0;	//resource id for each character drawable image
		int rIdTintColor = ResourceHelper.getColor("character_color"+tintColorChosen);
		int rIdAccentColor = ResourceHelper.getColor("accent_color"+accentColorChosen);
		int numCharacters = (int) context.getResources().getInteger(R.integer.character_count);
		for (int i=0; i<numCharacters; i++) {
			rIdTint = ResourceHelper.getImage("character"+i+"_tint");
			rIdAccent = ResourceHelper.getImage("character"+i+"_accent");
			rIdTop = ResourceHelper.getImage("character"+i+"_top");
			
			img.add(ProfilePicPageFragment.newInstance("",rIdTint, rIdAccent, rIdTop, rIdTintColor, rIdAccentColor));
		}
		return img;
	}
	
	/**
	 * Sets listeners and assign colors for all options in background color selection popup
	 * @param d The Dialog instance that contains the buttons with all color choices
	 * @param rName The name of the color resource corresponding to the proper color set
	 */
	public void setColorListeners(Dialog d, String rName) {
		GradientDrawable drawable;
		OnClickListener listener = (OnClickListener) new ProfileColorSelectedListener(d);
		Button current;
		int currentColor;
		int rIdButton=0, rIdColor=0; //resource identifiers for current button and color
		
		int numColors = (int) context.getResources().getInteger(R.integer.color_count);
		for (int i=0; i<numColors; i++){
			rIdButton = ResourceHelper.getId("profile_color_button"+i);
			rIdColor = ResourceHelper.getColor(rName+i);
			
			current = (Button) d.findViewById(rIdButton);
			currentColor = context.getResources().getColor(rIdColor);
			current.setOnClickListener(listener);
			//set tag as String
			//first character is button number
			//rest of String decides type of color (tint, bg, accent)
			current.setTag(i+rName);
			
			//change background color of button to match corresponding color choice
			drawable = (GradientDrawable) current.getBackground();
			drawable.setColor(currentColor);
		}
	}
	
	/**
	 * Listener class for all color selection buttons in background color popup menu
	 */
	private class ProfileColorSelectedListener implements OnClickListener{
		private Dialog dialog;
		
		public ProfileColorSelectedListener(Dialog d) {
			dialog = d;
		}
		
		@Override
		public void onClick(View v) {
			String rName = (String) v.getTag();
			int colorChosen = Character.getNumericValue(rName.charAt(0));//first character is button number
			//rest of String decides type of color (tint, bg, accent)
			if (rName.substring(1).equals("profile_color")) {
				bgColorChosen=colorChosen;
				setBackgroundColor(bgColorChosen);
			}
			else if (rName.substring(1).equals("accent_color")) {
				accentColorChosen=colorChosen;
				setAccentColor(accentColorChosen);
			}
			else { //"character_color"
				tintColorChosen=colorChosen;
				setTintColor(tintColorChosen);
			}
			dialog.dismiss();
		}
	}
	
	/**
	 * Changes the background color of the layout of current Activity
	 * @param color Integer representing chosen color (corresponds to colors defined in resources)
	 */
	private void setBackgroundColor(int color) {
		int rIdColor=ResourceHelper.getColor("profile_color"+color);
		layout.setBackgroundColor(context.getResources().getColor(rIdColor));
	}
	
	/**
	 * Changes the accent color of the currently displayed character
	 * Note that fragments only exist as needed so there is not a 
	 * way to change the color of all Character images at once. Instead so this on next or previous button press
	 * @param color Integer representing chosen color
	 * @param color Integer representing chosen color
	 */
	private void setAccentColor(int color) {
		int rIdColor=ResourceHelper.getColor("accent_color"+color);
		ImageView img;
		accentColorChosen=color;	//set global variable to update all fragments on "page turn"
		ViewPager pager = (ViewPager)layout.findViewById(R.id.profile_pic_gallery);
		ViewPagerAdapter adapter = (ViewPagerAdapter) pager.getAdapter();
		
		//tint the currently displayed image
		int i = pager.getCurrentItem();
		img = (ImageView) adapter.getItem(i).getView().findViewById(R.id.profile_img_accent);
		img.setColorFilter(context.getResources().getColor(rIdColor));
		
		//tint the paint brush button (for customization screen only)
		Button paint = (Button) layout.findViewById(R.id.button_character_color2);
		paint.getBackground().setColorFilter(context.getResources().getColor(rIdColor), PorterDuff.Mode.MULTIPLY);
	}
	
	public void setAccentColor() {
		setAccentColor(accentColorChosen);
		Log.d("UI","accent color is "+accentColorChosen);
	}
	
	
	/**
	 * Changes the tint color of the character.
	 * Note that fragments only exist as needed so there is not a 
	 * way to change the color of all Character images at once. Instead so this on next or previous button press
	 * @param color Integer representing chosen color
	 */
	public void setTintColor(int color) {
		int rIdColor=ResourceHelper.getColor("character_color"+color);
		ImageView img;
		tintColorChosen=color;	//set global variable to update all fragments on "page turn"
		ViewPager pager = (ViewPager)layout.findViewById(R.id.profile_pic_gallery);
		ViewPagerAdapter adapter = (ViewPagerAdapter) pager.getAdapter();
		
		//tint the currently displayed image
		int i = pager.getCurrentItem();
		img = (ImageView) adapter.getItem(i).getView().findViewById(R.id.profile_img_tint);
		img.setColorFilter(context.getResources().getColor(rIdColor));
		
		//tint the paint brush button (for customization screen only)
		Button paint = (Button) layout.findViewById(R.id.button_character_color1);
		paint.getBackground().setColorFilter(context.getResources().getColor(rIdColor), PorterDuff.Mode.MULTIPLY);
	}
	
	public void setTintColor() {
		setTintColor(tintColorChosen);
	}
	
	public int getBgColor(){
		return bgColorChosen;
	}
	
	public int getTintColor(){
		return tintColorChosen;
	}
	
	public int getAccentColor(){
		return accentColorChosen;
	}
	
	public Activity getContext(){
		return context;
	}
}
